<?php
class tgyuan_areaControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function onDefault(){
		$tgyid=get_post('tgyid','i');
		$tgyuan=M("tgyuan")->selectRow("id=".$tgyid);
		$sclist=M("site_city")->children(0);
		 
		$scids=M("tgyuan_area")->selectCols(array(
			"where"=>" tgyid=".$tgyid,
			"fields"=>"sc_id"
		));
		$saids=M("tgyuan_area")->selectCols(array(
			"fields"=>"sc_id",
			"where"=>" siteid=".SITEID
		));
		if($saids && $scids){
			foreach($saids as $k=>$v){
				if(in_array($v,$scids)){
					unset($saids[$k]);
				}
			}
		}
		 
			foreach($sclist as $k=>$v){
				if($v['child']){
					foreach($v['child'] as $kk=>$vv){
						if($scids){
							if(in_array($vv['sc_id'],$scids)){
								$vv['isdo']=1;
							}
						}
						
						
						$v['child'][$kk]=$vv;
						if($saids){
							if(in_array($vv['sc_id'],$saids)){
								unset($v['child'][$kk]);
							}
						}
					}
				}
				$sclist[$k]=$v;
			}
		 
		$this->smarty->assign(array(
			"sclist"=>$sclist,
			"tgyuan"=>$tgyuan,
			"scids"=>$scids
		));
		$this->smarty->display("tgyuan_admin/area.html");
	}
	
	public function onShouquan(){
		$sc_id=get_post('sc_id','i');
		$tgyid=get_post('tgyid','i');
		$row=M("tgyuan_area")->selectRow("sc_id=".$sc_id);
		if($row){
			$this->goALl("授权失败",1);
		}
		M("tgyuan_area")->insert(array(
			"siteid"=>SITEID,
			"dateline"=>time(),
			"sc_id"=>$sc_id,
			"tgyid"=>$tgyid,
			"shopid"=>$tgyid,
		));
		$this->goALl("授权成功");
	}
	
	public function onQxSHouquan(){
		$sc_id=get_post('sc_id','i');
		$tgyid=get_post('tgyid','i');
		M("tgyuan_area")->delete("sc_id=".$sc_id." AND tgyid=".$tgyid);
		$this->goALl("取消授权成功");
	}
	
}
?>